package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.pinda.DTO.transportline.TransportTripsTruckDriverDto;
import com.itheima.pinda.common.CustomIdGenerator;
import com.itheima.pinda.entity.PdTransportTripsTruckDriver;
import com.itheima.pinda.mapper.PdTransportTripsTruckDriverMapper;
import com.itheima.pinda.service.PdTransportTripsTruckDriverService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class PdTransportTripsTruckDriverServiceImpl implements PdTransportTripsTruckDriverService {

    @Autowired
    private CustomIdGenerator idGenerator;
    @Autowired
    private PdTransportTripsTruckDriverMapper transportTripsTruckDriverMapper;

    @Override
    @Transactional
    public void batchSave(List<TransportTripsTruckDriverDto> truckTransportTripsDto) {

        truckTransportTripsDto.stream().forEach(transportTripsTruckDriverDto -> {

            PdTransportTripsTruckDriver transportTripsTruckDriver = new PdTransportTripsTruckDriver();
            BeanUtils.copyProperties(transportTripsTruckDriverDto,transportTripsTruckDriver);
            transportTripsTruckDriver.setId(idGenerator.nextId(transportTripsTruckDriver) + "");
            transportTripsTruckDriverMapper.insert(transportTripsTruckDriver);
        });

    }

    @Override
    public List<TransportTripsTruckDriverDto> findAll(String transportTripsId, String truckId, String userId) {
        List<PdTransportTripsTruckDriver> transportTripsTruckDrivers = transportTripsTruckDriverMapper.selectList(
                new LambdaQueryWrapper<PdTransportTripsTruckDriver>()
                        .eq(!StringUtils.isEmpty(transportTripsId), PdTransportTripsTruckDriver::getTransportTripsId, transportTripsId)
                        .eq(!StringUtils.isEmpty(truckId), PdTransportTripsTruckDriver::getTruckId, truckId)
                        .eq(!StringUtils.isEmpty(userId), PdTransportTripsTruckDriver::getUserId, userId));

        List<TransportTripsTruckDriverDto> transportTripsTruckDriverDtos = transportTripsTruckDrivers.stream().map(pdTransportTripsTruckDriver -> {
            TransportTripsTruckDriverDto transportTripsTruckDriverDto = new TransportTripsTruckDriverDto();
            BeanUtils.copyProperties(pdTransportTripsTruckDriver, transportTripsTruckDriverDto);
            return transportTripsTruckDriverDto;
        }).collect(Collectors.toList());

        return transportTripsTruckDriverDtos;
    }
}
